//
//  PublicViewController.m
//  Tweetero
//
//  Created by Mac Admin on 15/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//


#import "PublicViewController.h"
#import "LoginController.h"
#import "MGTwitterEngine.h"
#import "ImageLoader.h"
#import "MessageViewController.h"
#import "MessageListController.h"
#import "TwitEditorController.h"
#import "TweetterAppDelegate.h"

#define NAME_TAG 1
#define TIME_TAG 2
#define IMAGE_TAG 3
#define TEXT_TAG 4
#define ROW_HEIGHT 70

@implementation PublicViewController

- (void)dealloc 
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}


- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	self.navigationItem.title = @"Yam Home";
	
	UIBarButtonItem *newMsgButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
																				  target:self action:@selector(newMessage)];
	self.navigationItem.rightBarButtonItem = newMsgButton;
	[newMsgButton release];
	
	UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
																				  target:self action:@selector(reload)];
	self.navigationItem.leftBarButtonItem = reloadButton;
	[reloadButton release];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:@"TwittsUpdated" object:nil];
	if([MGTwitterEngine password] == nil)
		[LoginController showModal:self.navigationController];
}


- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
}


- (void)accountChanged:(NSNotification*)notification
{
	[self reloadAll];
	self.navigationItem.title = [MGTwitterEngine username];
}

- (void)loadMessagesStaringAtPage:(int)numPage count:(int)count
{
	[super loadMessagesStaringAtPage:numPage count:count];
	if([MGTwitterEngine password] != nil)
	{
		[TweetterAppDelegate increaseNetworkActivityIndicator];
		[_twitter getPublicTimelineSinceID:(int)nil];
		self.navigationItem.title = [MGTwitterEngine username];
	}
}

- (NSString*)noMessagesString
{
	return @"No Tweets";
}

- (NSString*)loadingMessagesString
{
	return @"Loading Tweets...";
}

- (void)newMessage
{
	TwitEditorController *msgView = [[TwitEditorController alloc] init];
	[self.navigationController pushViewController:msgView animated:YES];
	[msgView release];
}

- (void)reload
{
	if([MGTwitterEngine password] == nil)
		[LoginController showModal:self.navigationController];
	else
		[self reloadAll];
}

@end

